package com.example.demo.算法_test;

import java.util.Stack;

public class ProfileTest {

    static class Node{
        int value;
        Node left;
        Node right;

        public Node(int value) {
            this.value = value;
        }
    }

    /**
     * 树形结构遍历
     * @param node
     */
    public static void process(Node node){
        if (node == null){
            return;
        }
        //
//        System.out.println("先pre-序值："+node.value);
        process(node.left);
        System.out.println("中mid-序值："+node.value);
        process(node.right);
//        System.out.println("后post-序值："+node.value);
    }

    public static void pre(Node head){
        if (head==null){
            return;
        }
        Stack<Node> stack = new Stack();
        stack.push(head);
        while (!stack.isEmpty()){
            Node pop = stack.pop();
            System.out.println(pop.value);
            if (pop.right!=null){
                stack.push(pop.right);
            }
            if (pop.left!=null){
                stack.push(pop.left);
            }
        }
    }
    public static void mid(Node cur) {
        if (cur == null) {
            return;
        }
        Stack<Node> stack = new Stack<>();
        while (!stack.isEmpty() || cur!=null){
            if (cur!=null){
                //  左 左 左 左 左 head
                stack.push(cur);
                cur = cur.left;
            }else{
                // 加入左节点不能再加的时候，就是弹出节点的时候了！
                // 当前节点为null，说明遍历到底部了，
                // 空了，就要弹出，站内元素
                // 左
                cur = stack.pop();
                // 处理中间
                System.out.println(cur.value);
                // 后入 后出 处理后面
                cur = cur.right;
            }
        }
    }

    public static void dealLeft(Node node, Stack<Node> stack) {
        if (node!=null && stack.peek()!=node){
            while (!stack.isEmpty()){

            }
        }
    }
    public static void post(Node node){
        if (node == null){
            return;
        }
        Stack<Node> stack = new Stack<>();
        Stack<Node> stack1 = new Stack<>();
        stack.push(node);
        while (!stack.isEmpty()){
            node = stack.pop();
            stack1.push(node);
            if (node.left!= null){
                stack.push(node.left);
            }
            if (node.right != null){
                stack.push(node.right);
            }
        }
        while (!stack1.isEmpty()){
            System.out.println(stack1.pop());
        }
    }


        public static void main(String[] args){
        // 生成树 ，并遍历树
        Node head = new Node(0);

        Node node01 = new Node(1);
        Node node02 = new Node(2);
        Node node03 = new Node(3);
        Node node04 = new Node(4);
        Node node05 = new Node(5);
        Node node06 = new Node(6);

        head.left = node01;
        head.right = node02;

        node01.left = node03;
        node01.right = node04;

        node02.left = node05;
        node02.right = node06;

            mid(head);

    }







}
